package cn.com.se.sharepictrue.utils;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2015/12/4.
 */
public class ImageUtils {

    /**
     * 将得到的uri转为绝对路径
     *
     * @param context 当前activity
     * @param uri     图片uri
     * @return 图片路径
     */
    public static String getRealFilePath(Context context, Uri uri) {

        String result;
        Cursor cursor = context.getContentResolver().query(uri, null, null,
                null, null);
        if (cursor == null) { // Source is Dropbox or other similar local file
            // path
            result = uri.getPath();
        } else {
            cursor.moveToFirst();
            int idx = cursor
                    .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(idx);
            cursor.close();
        }
        return result;
    }

    public static Bitmap getBitmapFromUri(Context context, Uri uri, int IMAGE_MAX_WIDTH, int IMAGE_MAX_HEIGHT) {
        try {
            InputStream input = context.getContentResolver().openInputStream(uri);
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(input, null, opts);
            input.close();

            if (opts.outHeight == -1 || opts.outWidth == -1)
                return null;
            if (opts.outHeight > opts.outWidth) {
                opts.inSampleSize = (int) (opts.outHeight / IMAGE_MAX_HEIGHT);

            } else {
                opts.inSampleSize = (int) (opts.outWidth / IMAGE_MAX_WIDTH);
            }
            if (opts.inSampleSize < 1)
                opts.inSampleSize = 1;
            input = context.getContentResolver().openInputStream(uri);
            opts.inJustDecodeBounds = false;
            Bitmap bm = BitmapFactory.decodeStream(input, null, opts);

            input.close();
            return cropImage(bm, IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            ToastUtils.show(context, "图片不存在");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            ToastUtils.show(context, "图片读取失败");
            e.printStackTrace();
        }
        return null;
    }

    public static Bitmap cropImage(Bitmap bm, int IMAGE_MAX_WIDTH, int IMAGE_MAX_HEIGHT) {
        // TODO Auto-generated method stub
        float w = bm.getWidth();
        float h = bm.getHeight();
        if (w > h && w > IMAGE_MAX_WIDTH) {
            h = h * (IMAGE_MAX_WIDTH / w);
            w = IMAGE_MAX_WIDTH;
            bm = Bitmap.createScaledBitmap(bm, (int) w, (int) h, true);
        } else if (h >= w && h > IMAGE_MAX_HEIGHT) {
            w = w * (IMAGE_MAX_HEIGHT / h);
            h = IMAGE_MAX_HEIGHT;
            bm = Bitmap.createScaledBitmap(bm, (int) w, (int) h, true);
        }
        return bm;
    }

}
